<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Datatask extends CrmController
{
    /**
     * dataTaskList
     */
    public function dataTaskList()
    {
        $menuname = "Data Task";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];
        $this->assign("priv",$priv);

        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 模板输出
        return $this->fetch('dataTaskList');
    }


    /**
     * 获取数据任务的数据
     * @return json 列表数据
     */
    public function dataTaskListData () 
    {
        $where = "(1 ";
        $where .= empty($_REQUEST["name"])?"":" AND name LIKE '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["createuser"])?"":" AND createuser = '".$_REQUEST["createuser"]."'";
        $where .= empty($_REQUEST["id"])?"":" AND id = '".$_REQUEST["id"]."'";
        $where .= empty($_REQUEST["start_createtime"])?"":" AND createtime > '".$_REQUEST["start_createtime"]."'";
        $where .= empty($_REQUEST["end_createtime"])?"":" AND createtime < '".$_REQUEST["end_createtime"]."'";
        $where .= " )";

        $arrData = Loader::model('Datatask')->dataTaskListData($where);
        echo json_encode($arrData);
    }


    /**
     * dataTaskDetail
     */
    public function dataTaskDetail ()
    {
        $menuname = "Data Task";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];
        $this->assign("priv",$priv);
        
        $this->assign("data_task",$_REQUEST['id']);
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 模板输出
        return $this->fetch('dataTaskDetail');
    }


    /**
     * 获取指定数据任务的客户
     * @return json 列表数据
     */
    public function dataTaskDetailData () 
    {
        $where = "(1 ";
        $where .= " AND data_task='".$_REQUEST['data_task']."'";
        $where .= empty($_REQUEST["username"])?"":" AND username = '".$_REQUEST["username"]."'";
        $where .= empty($_REQUEST["customer_name"])?"":" AND customer_name = '".$_REQUEST["customer_name"]."'";
        $where .= " )";

        $arrData = Loader::model('Datatask')->dataTaskDetailData($where);
        echo json_encode($arrData);
    }


    /**
     * 前台数据任务
     */
    public function serviceTaskList()
    {
        $menuname = "Data Task";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];
        $this->assign("priv",$priv);

        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 模板输出
        return $this->fetch('serviceTaskList');
    }


    /**
     * serviceTaskDetail
     */
    public function serviceTaskDetail ()
    {
        $menuname = "Data Task";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];
        $this->assign("priv",$priv);
        
        $this->assign("data_task",$_REQUEST['id']);
        $arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
        // 模板输出
        return $this->fetch('serviceTaskDetail');
    }


    /**
     * 获取指定数据任务的客户
     * @return json 列表数据
     */
    public function serviceTaskDetailData () 
    {
        $where = "(1 ";
        $where .= " AND data_task='".$_REQUEST['data_task']."'";
        $where .= " AND username = '".$_SESSION['user_info']["username"]."'";
        $where .= empty($_REQUEST["customer_name"])?"":" AND customer_name = '".$_REQUEST["customer_name"]."'";
        $where .= " )";

        $arrData = Loader::model('Datatask')->dataTaskDetailData($where);
        echo json_encode($arrData);
    }


    /**
     * 手动确定完成数据任务
     * @return json 完成状态
     */
    public function completeDataTask ()
    {
        $id = $_REQUEST['id'];
        $customer_code = $_REQUEST['customer_code'];
        Loader::model('Datatask')->completeDataTask($id, $customer_code);
    }



    /**
     * 渲染模板editCustomer
     */
    public function editCustomer()
    {
        $menuname = "Data Task";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $customer_code = $_REQUEST['customer_code'];
        if (empty($customer_code)) {
            echo "页面出现未知错误！";
            exit;
        }
        $where = 'b.customer_code="'.$customer_code.'"';
        // 基本资料
        $customerData = Loader::model('Customer')->getCustomerData($where);
        // echo $where;exit;
        $customerData = $customerData['data'][0];
        // echo "<pre>";
        // print_r($customerData);exit;
        if ($customerData['sex'] == "男") {
            $customerData['option'] = '<option value="1" selected>男</option> <option value="2">女</option>';
        } elseif ($customerData['sex'] == "女") {
            $customerData['option'] = '<option value="1">男</option> <option value="2" selected>女</option>';
        } else {
            $customerData['option'] = '<option value="1">男</option> <option value="2">女</option>';
        }


        $users = Cache::get('users');
        $customerData['createuser'] = $users[$customerData['create_user']]['cn_name']."（".$customerData['create_user']."）";

        /*echo "<pre>";
        print_r($customerData);exit;*/
        $this->assign('customerData', $customerData);

        $address = Loader::model('Customer')->getAddress($customer_code);
        $this->assign('address', $address);
        $this->assign('id', $_REQUEST['id']);
        // 模板输出
        return $this->fetch('editCustomer');
    }


    /**
     * 更新客户资料
     */
    public function updateCustomer () 
    {
        $data = $_REQUEST;
        $location_address = explode('/', $data['location_address']);
        $address['province'] = $location_address[0];
        $address['city'] = $location_address[1];
        $address['area'] = $location_address[2];
        $address['address_details'] = $data['address_details'];
        $address['customer_code'] = $data['customer_code'];
        $address_return = Loader::model('Customer')->updateAddressCustomer($address);
        // print_r($address);exit;

        $basic['customer_code'] = $data['customer_code'];
        if(strpos($data['phone'],"*") === false) {
            $basic['phone'] = $data['phone'];
        }
        if(strpos($data['telephone'],"*") === false) {
            $basic['telephone'] = $data['telephone'];
        }
        $basic['name'] = $data['name'];
        $basic_return = Loader::model('Customer')->updateBasicCustomer($basic);
        // print_r($basic);

        $detail['customer_code'] = $data['customer_code'];
        $detail['sex'] = $data['sex'];
        $detail['age'] = $data['age'];
        $detail['member_level'] = $data['member_level'];
        $detail_return = Loader::model('Customer')->updateDetailCustomer($detail);
        // print_r($detail);
        /*print_r($address_return);
        print_r($basic_return);
        print_r($detail_return);exit;*/
        if ($address_return || $basic_return || $detail_return) {
            echo json_encode(array('success'=>true,'msg'=>'保存成功！'));
        } else {
            echo json_encode(array('msg'=>'保存失败！'));
        }
    }

}